﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Helper.Core.Entity;
using System.Text;
using Helper.Business.Implementation;
using Helper.Infrastructure.Session;
using System.Collections;

namespace Helper.Controllers
{
    public partial class CallController : BaseController
    {
        [HttpGet]
        [ActionName("GetAll")]
        public JsonResult GetAll()
        {
            User userLogged = new UserRepository().FindById(UserSession.GetInstance.Id);
            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            IEnumerable dataReturn = _CallRepository.GetAllUserRelated(userLogged).Select(obj => new
            {
                Id = obj.Id,
                Subject = obj.Subject,
                Timestamp = obj.Timestamp.ToUniversalTime(),
                AuthorName = obj.Author.Name,
                AuthorParentBusinessName = obj.Author.ParentBusiness.Name,
                StatusId = obj.Posts.Count<Post>() > 0 ? obj.Posts.Last<Post>().Status.Id : 0,
                StatusName = obj.Posts.Count<Post>() > 0 ? obj.Posts.Last<Post>().Status.Name : "Não respondido",
                StatusDependency = obj.Posts.Count<Post>() > 0 ? ((int)obj.Posts.Last<Post>().Status.Dependency) : -1,
                StatusConclusive = obj.Posts.Count<Post>() > 0 ? obj.Posts.Last<Post>().Status.Conclusive : false
            }).ToList();
            result.Data = dataReturn;
            return result;

        }

        [HttpGet]
        public JsonResult GetCall(int Id, int Tab)
        {
            Call call = _CallRepository.FindById(Id);
            call.Posts = new PostRepository().GetAllByCall(Id);
            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            result.Data = _CallRepository.FindById(Id);
            return result;
        }

        [HttpGet]
        public ActionResult GetPreview(int Id)
        {
            Call call = _CallRepository.FindById(Id);
            call.Posts = new PostRepository().GetAllByCall(Id);
            return View(call);
        }
    }
}
